import env from "../../utils/env"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectPcId: "4",
    headType: 'visit_rate',
    bankList: [],
    personList: [],
    total: '',
    pageNum: 1,
    pageSize: 8,
    selectPc: '2023年07月-第四批次',
    pickerList: [
      // {
      //   id: '0',
      //   name: '全部'
      // },
      {
        id: '1',
        name: '2023年03月-第一批次'
      },
      {
        id: '2',
        name: '2023年05月-第二批次'
      },
      {
        id: '3',
        name: '2023年06月-第三批次'
      },
      {
        id: '4',
        name: '2023年07月-第四批次'
      },
      {
        id: '5',
        name: '2023年08月-第五批次'
      },
      {
        id: '6',
        name: '2023年08月-第六批次'
      },
      {
        id: '7',
        name: '2023年10月-第七批次'
      }, {
        id: '8',
        name: '2023年12月-第八批次'
      }, {
        id: '9',
        name: '2024年01月-第九批次'
      },{
        id: '10',
        name: '2024年02月-第十批次'
      },{
        id: '11',
        name: '2024年03月-第十一批次'
      },
      {
        id: '12',
        name: '2024年03月-第十二批次'
      },
    ],
    statusBarHeight: '',
    navBarHeight: '',
    listType: 1,
    searchValue: '',
    showSelect: false,
  },
  goSearch(e) {
    console.log(e, '搜搜的内容')
    this.setData({
      searchValue: e.detail
    })
    this.setData({
      bankList: [],

    })
    this.getList()
  },
  topcheck(e) {
    console.log(e.currentTarget.dataset.type, 'eee')
    this.setData({
      searchValue: '',
      listType: e.currentTarget.dataset.type,
      bankList: [],
      personList: [],
    })
    this.getList()
  },
  goback() {
    wx.navigateBack({
      delta: 1
    })
  },
  onChange(e) {

    // console.log(e,'ee')
    const id = this.data.pickerList[Number(e.detail.value)].id
    // this.data.bankList=[]
    this.setData({

      pageNum: 1,
      bankList: [],
      selectPcId: id,
      selectPc: this.data.pickerList[Number(e.detail.value)].name
    })
    // 重新查询
    this.getList()

  },
  getList() {
    if (this.data.listType == 1) {
      const params = {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        type: this.data.headType,
        batchNum: this.data.selectPcId,
        name: '工商'
      }
      console.log(params, 'params')
      getApp().globalData.request(
        '/corpVisit/queryLaborRaceBankRank', { name: this.data.searchValue },
        "POST"
      ).then(res => {
        console.log(res, '团体成绩接口');

        const list = this.data.bankList.concat(res.data)
        // 处理进度

        this.setData({
          bankList: list,
          total: res.data.total
        })
      })
    } else {
      getApp().globalData.request(
        '/corpVisit/queryLaborRaceTop10', { name: this.data.searchValue },
        "POST"
      ).then(res => {
        console.log(res, '个人成绩前10接口');
        const personList = this.data.bankList.concat(res.data)
        // 处理进度

        this.setData({
          personList: personList,
          total: res.data.total
        })
        console.log(this.data.personList, '个人排名')
      })
    }
  },
  hideSelect() {
    this.setData({
      showSelect: false,
    });
  },
  goExport() {
    this.setData({
      showSelect: true,
    });
    this.download();
  },
  async downloadWx() {
    const _this = this
    wx.shareFileMessage({
      filePath: _this.data.FilePath,
      fileName: '竞赛成绩.xls',
      success(data) {
        _this.hideSelect()
      },
      fail(res) {
        console.log(res, '分享报错的原因')
      }
    })
  },
  download() {
    wx.showLoading({
      title: "导出中...",
    });
    const _this = this
    const params = {
    }
    console.log(params, '导出的传参')
    return new Promise(function (resolve, reject) {
      wx.request({
        // url: 'http://192.168.1.200:20100/visit/visitStatisticsExport',
        // url: 'https://fintech-wx.szjrgzjg.com/visit/visitStatisticsExport',
        // url: env.baseUrl + '/corpVisit/export_bank_statistics',
        url: env.baseUrl + '/corpVisit/labor_statistics_export',
        data: params,
        method: 'POST',
        header: {
          "content-type": "application/json",
          "token": wx.getStorageSync('token')
        },
        responseType: "arraybuffer", //注意这里的responseType
        success: (result) => {
          console.log(result, '接口调用成功')
          let fileManager = wx.getFileSystemManager();
          _this.data.FilePath = wx.env.USER_DATA_PATH + "/" + '竞赛成绩.xlsx';
          console.log(_this.data.FilePath, 'FilePath')
          fileManager.writeFile({
            data: result.data,
            filePath: _this.data.FilePath,
            encoding: "binary", //编码方式 
            success: res => {
              console.log("下载成功！", result);
              wx.hideLoading();
              resolve('1111')
              // 电脑测试打开下面的注释代码
              // wx.openDocument({ 
              //   filePath: _this.data.FilePath,
              //   showMenu: true,
              //   fileType: "xls",
              //   success: result => {
              //     _this.setData({
              //       loadingHidden: true,
              //     })
              //     console.log("打开文档成功");
              //   },
              //   fail: err => {
              //     this.setData({
              //       loadingHidden: true,
              //     })
              //     console.log("打开文档失败", err);
              //   }
              // });
            },
            fail: res => {
              wx.showToast({
                title: '下载失败!',
                icon: 'none', //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
                duration: 2000, //停留时间
              })
            }
          })
        },
        fail(err) {
          console.log(err)
        }
      })
    })
  },
  nextLeve(e) {
    console.log(e, '点击查询')
    if (this.data.listType == '1') {
      let bankId = e.currentTarget.dataset.bankid
      let myLeve = e.currentTarget.dataset.myleve
      wx.navigateTo({
        url: `../../sub_qiangdan/internalRanking/internalRanking?bankId=${bankId}&myLeve=${myLeve}`
        // url: '../../sub_qiangdan/internalRanking/internalRanking'
      });
    } else {
      let userId = e.currentTarget.dataset.userid
      console.log(userId, 'userIduserId')
      wx.navigateTo({
        url: `/sub_qiangdan/performanceRank/performanceRank?userId=${userId}`
      })
    }



  },
  headClick(e) {
    this.setData({
      headType: e.currentTarget.dataset.type
    })
    console.log(this.data.headType, '111')
    this.setData({
      pageNum: 1,
      bankList: [],
    })
    this.getList()
  },
  scrollBottom() {
    if (this.data.bankList.length < this.data.total) {
      // this.onBottom();
      this.setData({
        pageNum: ++this.data.pageNum,
      });
      this.getList();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取系统信息， 状态栏高度为 sysInfo['statusBarHeight'];
    let sysInfo = wx.getSystemInfoSync();
    // 胶囊信息
    let menu = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度
    let navBarHeight = (menu.top - sysInfo.statusBarHeight) * 2 + menu.height;
    this.setData(({
      statusBarHeight: sysInfo.statusBarHeight,
      navBarHeight: navBarHeight
    }))
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})